package com.example.shiyan3

import android.media.Image
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_content.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.titlefragment.*
import org.w3c.dom.Text

class TitleFragment:Fragment() {
    private var isTwoPane = false

    val imgarray = arrayListOf(
        arrayListOf(R.drawable.airpods,R.drawable.airpods1,R.drawable.airpods2,R.drawable.airpods3),
        arrayListOf(R.drawable.huawei,R.drawable.huawei1,R.drawable.huawei2,R.drawable.huawei3),
        arrayListOf(R.drawable.iphone13,R.drawable.iphone1,R.drawable.iphone2,R.drawable.iphone3),
        arrayListOf(R.drawable.xiaomi,R.drawable.xiaomi1,R.drawable.xiaomi2,R.drawable.xiaomi3)
    )
    val namearray = arrayListOf(
        "Apple AirPods Pro 配MagSafe无线充电盒 主动降噪无线蓝牙耳机 适用iPhone/iPad/Apple Watch",
        "HUAWEI P50 Pro 4G全网通 原色双影像单元 麒麟9000芯片 万象双环设计 8GB+256GB曜金黑华为手机",
        "Apple iPhone 13 Pro Max (A2644) 256GB 远峰蓝色 支持移动联通电信5G 双卡双待手机",
        "小米电视 A32 32英寸 立体声扬声器 64位处理器 享空间私享影音 智能网络教育电视 L32R6-A 红米Redmi 电视"
    )
    val pricearray = arrayListOf(
        1499.00f,6488.00f,9799.00f,699.00f
    )
    val bankaarray = arrayListOf(
        "首开PLUS年卡，预估此单可省52.00元",
        "首开PLUS年卡，预估此单可省89.00元",
        "首开PLUS年卡，预估此单可省75.00元",
        "首开PLUS年卡，预估此单可省20.00元"
    )

    val commentarray = arrayListOf(
        "12万好评 98%好评",
        "10万+评论 98%好评",
        "20万+评论 速抢！",
        "臻选好货，嗨购全场"
    )

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.titlefragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        isTwoPane = activity?.findViewById<View>(R.id.goodsContentLayout) != null
        val layoutManager = LinearLayoutManager(activity)
        TitleRecyclerView.layoutManager = layoutManager
        val adapter = NewsAdapter(getNews())
        TitleRecyclerView.adapter = adapter
        if(isTwoPane){
            Log.d("判断","这是双页的")
        }else{
            Log.d("判断","这是单页的")
        }
    }

    private fun getNews(): List<goods> {
        val goodsList = ArrayList<goods>()
        goodsList.add(goods(imgarray[0],namearray[0],pricearray[0],"京东自营",bankaarray[0],"Apple产品京东自营旗舰店"))
        goodsList.add(goods(imgarray[1],namearray[1],pricearray[1],"分期免息",bankaarray[1],"华为官方旗舰店"))
        goodsList.add(goods(imgarray[2],namearray[2],pricearray[2],"京东自营",bankaarray[2],"Apple产品京东自营旗舰店"))
        goodsList.add(goods(imgarray[3],namearray[3],pricearray[3],"线下同款",bankaarray[3],"小米官方旗舰店"))
        return goodsList
    }


    inner class NewsAdapter(val goodsList: List<goods>) : RecyclerView.Adapter<NewsAdapter.ViewHolder>() {

        inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            val name: TextView = view.findViewById(R.id.name)
            val img: ImageView = view.findViewById(R.id.goodsimg)
            val price: TextView = view.findViewById(R.id.price)
            val sort: TextView = view.findViewById(R.id.sort)
            val comment : TextView = view.findViewById(R.id.comment)
            val store : TextView = view.findViewById(R.id.store)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.goods_item, parent, false)
            val holder = ViewHolder(view)
            holder.itemView.setOnClickListener {
                val news = goodsList[holder.adapterPosition]
                if (isTwoPane) {
                    Log.d("titleFragment","这是双页的")
                    // 如果是双页模式，则刷新NewsContentFragment中的内容
                    val fragment = goodsContentFrag as contentFragment
                    fragment.refresh(news.img, news.name,news.price.toString(),news.sort,news.comment,news.store)
                } else {
                    // 如果是单页模式，则直接启动NewsContentActivity
                    Log.d("titleFragment","这是单页的")
                    contentActivity.actionStart(parent.context, news.img, news.name,news.price,news.sort,news.comment,news.store);
                }
            }
            return holder
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val goods = goodsList[position]
            holder.name.text = goods.name
            holder.img.setImageResource(goods.img[0])
            holder.price.text ="￥"+goods.price.toString()
            holder.comment.text = commentarray[position]
            holder.sort.text = goods.sort
            holder.store.text = goods.store
            holder.price.setTextColor(android.graphics.Color.RED)
            holder.sort.setBackgroundColor(android.graphics.Color.RED)
            /*
            *img.setImageResource(goods.img)
            name.text=goods.name
            price.text="￥"+goods.price.toString()
            sort.text=goods.sort
            comment.text=goods.comment
            store.text=goods.store
            price.setTextColor(android.graphics.Color.RED)
            sort.setBackgroundColor(android.graphics.Color.RED)
             */
        }

        override fun getItemCount() = goodsList.size

    }
}